<template>
  <el-row class="filter">
    <el-col :span="5">
      <div class="title">
        <span>价格</span>
        <span>0 - {{ price }}</span>
      </div>
      <el-slider v-model="price" :max="4000" @change="changePrice"></el-slider>
    </el-col>
    <el-col :span="4">
      住宿等级
      <el-dropdown @command="handleCommandLevel">
        <span class="el-dropdown-link">
          <span v-if="levelArr.length === 0">不限</span>
          <span v-if="levelArr.length === 1">{{ levelArr[0] }}</span>
          <span v-if="levelArr.length > 1">已选{{ levelArr.length }}项</span>
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            v-for="item of options.levels"
            :key="item.id"
            :command="{ id: item.level, name: item.name }"
          >
            <div class="dropdown-item">
              <i
                class="el-icon-circle-check"
                v-if="str.indexOf('hotellevel_in=' + item.level) > 0"
              ></i>
              <span v-else class="cilcle"></span>
              {{ item.name }}
            </div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-col>
    <el-col :span="4"
      >住宿类型
      <el-dropdown @command="handleCommandType">
        <span class="el-dropdown-link">
          <span v-if="typeArr.length === 0">不限</span>
          <span v-if="typeArr.length === 1">{{ typeArr[0] }}</span>
          <span v-if="typeArr.length > 1">已选{{ typeArr.length }}项</span>
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            v-for="item of options.types"
            :key="item.id"
            :command="{ id: item.id, name: item.name }"
          >
            <div class="dropdown-item">
              <i
                class="el-icon-circle-check"
                v-if="str.indexOf('hoteltype_in=' + item.id) > 0"
              ></i>
              <span v-else class="cilcle"></span>
              {{ item.name }}
            </div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-col>
    <el-col :span="4"
      >酒店设施
      <el-dropdown @command="handleCommandAsset">
        <span class="el-dropdown-link">
          <span v-if="assetArr.length === 0">不限</span>
          <span v-if="assetArr.length === 1">{{ assetArr[0] }}</span>
          <span v-if="assetArr.length > 1">已选{{ assetArr.length }}项</span>
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            v-for="item of options.assets"
            :key="item.id"
            :command="{ id: item.id, name: item.name }"
          >
            <div class="dropdown-item">
              <i
                class="el-icon-circle-check"
                v-if="str.indexOf('hotelasset_in=' + item.id) > 0"
              ></i>
              <span v-else class="cilcle"></span>
              {{ item.name }}
            </div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-col>
    <el-col :span="4"
      >酒店品牌
      <el-dropdown @command="handleCommandBrand">
        <span class="el-dropdown-link">
          <span v-if="brandArr.length === 0">不限</span>
          <span v-if="brandArr.length === 1">{{ brandArr[0] }}</span>
          <span v-if="brandArr.length > 1">已选{{ brandArr.length }}项</span>
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            v-for="item of options.brands"
            :key="item.id"
            :command="{ id: item.id, name: item.name }"
          >
            <div class="dropdown-item">
              <i
                class="el-icon-circle-check"
                v-if="str.indexOf('hotelbrand_in=' + item.id) > 0"
              ></i>
              <span v-else class="cilcle"></span>
              {{ item.name }}
            </div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-col>
    <el-col :span="3"
      ><el-button @click="cancelCondition" type="primary">撤销条件</el-button>
    </el-col>
  </el-row>
</template>

<script>
export default {
  name: "HotelFilter",
  data() {
    return {
      price: 4000,
      options: {},
      params: {
        city: this.$route.query.city,
        price_lt: 4000,
      },
      str: "",
      levelArr: [],
      typeArr: [],
      assetArr: [],
      brandArr: [],
    };
  },
  watch: {
    $route() {
      this.params.city = this.$route.query.city;
    },
  },
  created() {
    this.getOptions();
  },
  methods: {
    changePrice(price) {
      this.params.price_lt = price;
      this.$router.push({
        path: "/hotel",
        query: this.params,
      });
    },
    handleCommandLevel(level) {
      this.filterOptions(level, "hotellevel_in", this.levelArr);
    },
    handleCommandType(type) {
      this.filterOptions(type, "hoteltype_in", this.typeArr);
    },
    handleCommandAsset(asset) {
      this.filterOptions(asset, "hotelasset_in", this.assetArr);
    },
    handleCommandBrand(brand) {
      this.filterOptions(brand, "hotelbrand_in", this.brandArr);
    },
    // 撤销条件
    cancelCondition() {
      this.str = "";
      (this.levelArr = []),
        (this.typeArr = []),
        (this.assetArr = []),
        (this.brandArr = []),
        this.$router.push(`/hotel?city=${this.params.city}`);
    },
    filterOptions(option, key, arr) {
      const index = arr.indexOf(option.name);
      if (index > -1) {
        arr.splice(index, 1);
      } else {
        arr.push(option.name);
      }

      if (this.str.indexOf(`${key}=${option.id}`) > 0) {
        this.str = this.str.replace(`&${key}=${option.id}`, "");
      } else {
        this.str = this.str + `&${key}=${option.id}`;
      }
      let url = `/hotel?city=${this.params.city}&price_lt=${this.params.price_lt}${this.str}`;
      this.$router.push(url);
    },
    getOptions() {
      this.$axios.get("/hotels/options").then((res) => {
        this.options = res.data.data;
      });
    },
  },
};
</script>

<style scoped lang="less">
.filter {
  margin: 20px 0;
  border: 1px solid #ddd;
  padding: 10px 0;
  color: #666;
  .el-col {
    height: 68px;
    border-right: 1px solid #ddd;
    padding: 5px 20px;
    &:last-child {
      border: none;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .title {
      font-size: 14px;
      display: flex;
      justify-content: space-between;
    }
    .el-dropdown {
      width: 100%;
      font-size: 13px;
      margin-top: 10px;
      .el-dropdown-link {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }
  }
}
.el-dropdown-menu {
  width: 170px;
  height: 220px;
  overflow: auto;
}
.cilcle {
  width: 10px;
  height: 10px;
  display: inline-block;
  border-radius: 50%;
  border: 1px solid #000;
  margin-right: 5px;
  margin-left: 2px;
}
</style>
